<?php

Yii::import('application.models._base.BaseAuctions');

class WonAuction extends BaseAuctions {
	
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	/**
	 * (non-PHPdoc)
	 * @see BaseAuctions::relations()
	 */
	public function relations(){
		return CMap::mergeArray(parent::relations(), array(
			'userOrder' => array(self::HAS_ONE, 'UserOrder', 'auction_id'),
		));
	}
	
	public function search() {
		$criteria = new CDbCriteria;
// 		$criteria->with = array('userOrder');
		$criteria->addNotInCondition('userOrder.id', array(null));
		$criteria->compare('t.winner_id', $this->winner_id);
		$criteria->compare('t.status', 2);
// 		$criteria->compare('userOrder.type1', 1);
		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
			'sort' => array(
				'defaultOrder' => 'userOrder.date_won desc',
			),
		));
	}
	
	public function showPayNow(){
		$orderinfo = Orders::model()->find('auction_id = '.$this->id.' and customer_id = '.$this->last_bid_customer_id);
		$orderstatus = 0;
		if(!empty($orderinfo)){
			$orderstatus = $orderinfo->status;
		}
		/*
		if($this->id == 101249){
			$this->expires_time = 1390984525; 
		}
		*/
		if(time()-$this->expires_time>=$this->paynow_expired_time || $orderstatus != 1){
			return false;
		}else{
			return true;
		}
	}
	
}

